<template>
  <div class="login-container">
    <div class="login-box">
      <div class="illustration-wrapper hide-on-mobile">
        <div class="illustration-content">
          <h2 class="welcome-text">欢迎回来</h2>
          <p class="slogan">高效、安全的后台管理系统</p>
          <div class="illustration"></div>
        </div>
      </div>
      <div class="login-content">
        <div class="login-header">
          <h3 class="title">{{ title }}</h3>
          <p class="subtitle">请登录您的账号</p>
        </div>
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              size="large"
              auto-complete="off"
              placeholder="账号"
              class="custom-input"
            >
              <template #prefix>
                <svg-icon icon-class="user" class="input-icon"/>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              size="large"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter="handleLogin"
              class="custom-input"
            >
              <template #prefix>
                <svg-icon icon-class="password" class="input-icon"/>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <div class="captcha-container">
              <el-input
                v-model="loginForm.code"
                size="large"
                auto-complete="off"
                placeholder="验证码"
                class="custom-input captcha-input"
                @keyup.enter="handleLogin"
              >
                <template #prefix>
                  <svg-icon icon-class="validCode" class="input-icon"/>
                </template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </div>
          </el-form-item>
          <div class="remember-forgot">
            <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
            <!-- <a href="javascript:;" class="forgot-password">忘记密码</a> -->
          </div>
          <el-form-item class="login-btn-container">
            <el-button
              :loading="loading"
              size="large"
              type="primary"
              class="login-button"
              @click.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
          <div class="register-link" v-if="register">
            <span>还没有账号?</span>
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2025 柯艾凯 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import {getCodeImg, registerConfig} from "@/api/login";
import Cookies from "js-cookie";
import {encrypt, decrypt} from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const router = useRouter();
const {proxy} = getCurrentInstance();
const title = ref(import.meta.env.VITE_APP_TITLE || 'RuoYi-Vue-Lite');

const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
});

const loginRules = {
  username: [{required: true, trigger: "blur", message: "请输入您的账号"}],
  password: [{required: true, trigger: "blur", message: "请输入您的密码"}],
  code: [{required: true, trigger: "change", message: "请输入验证码"}]
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, {expires: 30});
        Cookies.set("password", encrypt(loginForm.value.password), {expires: 30});
        Cookies.set("rememberMe", loginForm.value.rememberMe, {expires: 30});
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore.login(loginForm.value).then(() => {
        router.push({path: redirect.value || "/"});
      }).catch(() => {
        loading.value = false;
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode();
        }
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.data.img;
      loginForm.value.uuid = res.data.uuid;
    }
  });
}

function getRegisterConfig() {
  registerConfig().then(res => {
    register.value = res.data
  })
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCode();
getCookie();
getRegisterConfig();

</script>

<style lang='scss' scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: #f2f3f5  url("../assets/images/login-background.svg");
  position: relative;
  overflow: hidden;
  z-index: 0;
}

.login-box {
  display: flex;
  width: 900px;
  height: 600px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 1;
  backdrop-filter: blur(10px);
  animation: fadeIn 0.8s ease-in-out;

  @media (max-width: 992px) {
    width: 90%;
    max-width: 450px;
    height: auto;
    min-height: 500px;
  }

  @media (max-width: 576px) {
    width: 95%;
    max-width: 350px;
    min-height: 450px;
  }
}

.illustration-wrapper {
  flex: 1;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
  position: relative;
  overflow: hidden;
}

.hide-on-mobile {
  @media (max-width: 992px) {
    display: none;
  }
}

.illustration-content {
  text-align: center;
  z-index: 2;
}

.welcome-text {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 10px;
  animation: slideUp 0.8s ease-in-out;
}

.slogan {
  font-size: 16px;
  opacity: 0.9;
  margin-bottom: 30px;
  animation: slideUp 0.8s ease-in-out 0.2s both;
}

.illustration {
  width: 280px;
  height: 280px;
  background-image: url("../assets/images/login-background.svg");
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  margin: 0 auto;
  animation: float 6s ease-in-out infinite;
}

.login-content {
  flex: 1;
  padding: 50px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;

  @media (max-width: 992px) {
    flex: 0 0 100%;
    padding: 40px 30px;
  }

  @media (max-width: 576px) {
    padding: 30px 20px;
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  animation: fadeIn 0.8s ease-in-out 0.3s both;

  @media (max-width: 576px) {
    margin-bottom: 30px;
  }
}

.title {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin-bottom: 10px;

  @media (max-width: 576px) {
    font-size: 24px;
  }
}

.subtitle {
  font-size: 16px;
  color: #666;

  @media (max-width: 576px) {
    font-size: 14px;
  }
}

.login-form {
  animation: fadeIn 0.8s ease-in-out 0.5s both;
}

.custom-input {
  border-radius: 8px;
  height: 50px;

  :deep(.el-input__wrapper) {
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.05);
    padding: 0 15px;
    transition: all 0.3s;

    &:hover, &:focus {
      box-shadow: 0 3px 12px rgba(103, 126, 234, 0.2);
    }
  }

  :deep(.el-input__inner) {
    height: 50px;
  }
}

.input-icon {
  font-size: 18px;
  color: #667eea;
}

.captcha-container {
  display: flex;
  gap: 10px;

  @media (max-width: 576px) {
    gap: 5px;
  }
}

.captcha-input {
  flex: 1;
}

.login-code {
  height: 50px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.05);

  img {
    height: 100%;
    cursor: pointer;
    transition: transform 0.3s;

    &:hover {
      transform: scale(1.05);
    }
  }
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 15px 0 25px;

}

.remember-me {
  color: #666;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s;

  &:hover {
    color: #764ba2;
    text-decoration: underline;
  }
}

.login-btn-container {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 50px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08);
  }

  &:active {
    transform: translateY(1px);
  }
}

.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;

  .link-type {
    color: #667eea;
    margin-left: 5px;
    text-decoration: none;
    font-weight: 600;
    transition: color 0.3s;

    &:hover {
      color: #764ba2;
      text-decoration: underline;
    }
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(0, 0, 0, 0.45);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 1;
  backdrop-filter: blur(5px);

  @media (max-width: 576px) {
    font-size: 10px;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-15px);
  }
  100% {
    transform: translateY(0px);
  }
}
</style>
